package net.mihome.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.activity.ChatActivity;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.R;
import net.mihome.app.adapter.ChatRoomListAdapter;
import net.mihome.app.bean.ChatRoom;
import net.mihome.app.widget.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class MihomeActivisActivity extends BaseActivity {
	private Button addActivityBtn;
	private View backView;
	private Button myActivityBtn;
	private Button allActivityBtn;
	private TextView titleTextView;
	private View refreshView;
//	private View activitisView;
	private ListView activitysListView;
	private ProgressBar progressBar;
	private ChatRoomListAdapter chatRoomListAdapter;
	private List<ChatRoom> rooms=new ArrayList<ChatRoom>();
	private int begin=0;
	private int num=100;
	private Handler handler;
	private AppContext appContext;
	private int listType=ChatRoom.ALL;
	public final static  int HANDER_MSG_START_PROGRESSBAR=333;
	public final static  int HANDER_MSG_STP_PROGRESSBAR=444;
	public final static  int HANDER_MSG_INITDATA=555;
	public final static  int HANDER_MSG_EXCEPT=666;
	public final static  int HANDER_MSG_MY_INITDATA=777;
	public final static int SHOW_LOADING_DIALOG=888;
	public final static int CLOSE_LOADING_DIALOG=999;
	private LoadingDialog loadingDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mihome_activity_list);
		initView();
	}
	public void initView(){
		progressBar=(ProgressBar) findViewById(R.id.chat_room_list_head_progress);
		progressBar.setVisibility(View.GONE);
		appContext=(AppContext) getApplication();
		titleTextView=(TextView) findViewById(R.id.chat_record_title);
		myActivityBtn=(Button) findViewById(R.id.activity_my_btn);
		myActivityBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myActivityBtn.setEnabled(false);
				allActivityBtn.setEnabled(true);
				titleTextView.setText("我的活动");
				listType=ChatRoom.MY;
				initData();
			}
		});
		allActivityBtn=(Button) findViewById(R.id.activity_all_btn);
		allActivityBtn.setEnabled(false);
		listType=ChatRoom.ALL;
		allActivityBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				allActivityBtn.setEnabled(false);
				myActivityBtn.setEnabled(true);
				titleTextView.setText("所有活动");
				listType=ChatRoom.ALL;
				initData();
			}
		});
		addActivityBtn=(Button) findViewById(R.id.activity_add_btn);
		addActivityBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(MihomeActivisActivity.this,MihomeActivisAddActivity.class);
				startActivity(intent);
			}
		});
		backView=findViewById(R.id.mihome_activity_list_back);
		backView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MihomeActivisActivity.this.finish();
			}
		});
		refreshView=findViewById(R.id.news_detail_refresh);
//		activitisView=findViewById(R.id.mihome_activitis_msg);
//		activitisView.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				Intent intent=new Intent(MihomeActivisActivity.this,MihomeActivisMsgActivity.class);
//				startActivity(intent);
//			}
//		});
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				switch (arg0.what) {
				case HANDER_MSG_START_PROGRESSBAR:{
//					progressBar.setVisibility(View.VISIBLE);
//					refreshView.setVisibility(View.GONE);
					break;
				}
				case HANDER_MSG_STP_PROGRESSBAR:{
//					progressBar.setVisibility(View.GONE);
//					refreshView.setVisibility(View.VISIBLE);
					break;
				}
				case HANDER_MSG_INITDATA:{
					initData();
					break;
				}
				case HANDER_MSG_MY_INITDATA:{
					listType=ChatRoom.MY;
					initData();
					break;
				}
				case SHOW_LOADING_DIALOG:{
					showLoadingDialog();
					break;
				}
				case CLOSE_LOADING_DIALOG:{
					cancleLoadingDialog();
					break;
				}
				default:
					chatRoomListAdapter = new ChatRoomListAdapter(MihomeActivisActivity.this, rooms,handler,listType);
					activitysListView.setAdapter(chatRoomListAdapter);
					chatRoomListAdapter.notifyDataSetChanged();
					break;
				}
				return false;
			}
		});
		activitysListView=(ListView) findViewById(R.id.chat_room_list_listview);

		activitysListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
				ChatRoom chatRoom=(ChatRoom) view.getTag();
				Intent intent=new Intent(MihomeActivisActivity.this,ChatActivity.class);
				intent.putExtra("room_id",chatRoom.getId() );
				intent.putExtra("room_name",chatRoom.getName() );
				startActivity(intent);
			}
		});
		initData();
	}
	/**
	 * 获取房间数据
	 * */
	public void initData(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					handler.sendEmptyMessage(SHOW_LOADING_DIALOG);
					rooms=appContext.getChatRoomList(begin, num,listType);
					handler.sendEmptyMessage(1);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					Toast.makeText(MihomeActivisActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
				handler.sendEmptyMessage(CLOSE_LOADING_DIALOG);
			}
		}).start();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}
	public void showLoadingDialog(){
		if(this.loadingDialog!=null){
			this.loadingDialog.dismiss();
		}else{
			this.loadingDialog=new LoadingDialog(this);
		}
		this.loadingDialog.show();
	}
	public void cancleLoadingDialog(){
		if(this.loadingDialog!=null&&this.loadingDialog.isShowing()){
			this.loadingDialog.dismiss();
		}
	}
}
